﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using UtilZ.Dotnet.Ex.Json;

namespace UtilZ.Dotnet.WindowsDesktopEx.NativeMethod
{
    /// <summary>
    /// 窗口位置信息
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct WindowLPRECT
    {
        /// <summary>
        /// 窗口离桌面左边距离
        /// </summary>
        public int Left;

        /// <summary>
        /// 窗口离桌面顶部距离
        /// </summary>
        public int Top;

        /// <summary>
        /// 窗口离桌面右边距离
        /// </summary>
        public int Right;

        /// <summary>
        /// 窗口离桌面底部距离
        /// </summary>
        public int Bottom;


        /// <summary>
        /// 宽度
        /// </summary>
        public int Width
        {
            get
            {
                return this.Right - this.Left;
            }
        }

        /// <summary>
        /// 高度
        /// </summary>
        public int Height
        {
            get
            {
                return this.Bottom - this.Top;
            }
        }
    }

    /// <summary>
    /// 获取 Win32 窗口的一些基本信息。
    /// </summary>
    public class WindowInfo
    {
        /// <summary>
        /// 
        /// </summary>
        public WindowInfo()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="className"></param>
        /// <param name="title"></param>
        /// <param name="isVisible"></param>
        /// <param name="bounds"></param>
        /// <param name="proId"></param>
        public WindowInfo(IntPtr hWnd, string className, string title, bool isVisible, Rectangle bounds, int proId)
        {
            Hwnd = hWnd;
            ClassName = className;
            Title = title;
            IsVisible = isVisible;
            Bounds = bounds;
            ProcessId = proId;
        }

        /// <summary>
        /// 获取窗口句柄。
        /// </summary>
        public IntPtr Hwnd { get; set; }

        /// <summary>
        /// 获取窗口类名。
        /// </summary>
        public string ClassName { get; set; }

        /// <summary>
        /// 获取窗口标题。
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 获取当前窗口是否可见。
        /// </summary>
        public bool IsVisible { get; set; }

        /// <summary>
        /// 获取窗口当前的位置和尺寸。
        /// </summary>
        public Rectangle Bounds { get; set; }

        /// <summary>
        /// 该窗口所属性进程ID
        /// </summary>
        public int ProcessId { get; set; }

        /// <summary>
        /// 获取窗口当前是否是最小化的。
        /// </summary>
        [JsonIgnore]
        public bool IsMinimized
        {
            get { return Bounds.Left == -32000 && Bounds.Top == -32000; }
        }


        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Title;
        }
    }

    /// <summary>
    /// 窗口闪烁结构信息
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct FLASHWINFO
    {
        /// <summary>
        /// The size of the structure in bytes.
        /// </summary>
        public uint cbSize;

        /// <summary>
        /// A Handle to the Window to be Flashed. The window can be either opened or minimized.
        /// </summary>
        public IntPtr hwnd;

        /// <summary>
        /// The Flash Status.
        /// </summary>
        public uint dwFlags;

        /// <summary>
        /// The number of times to Flash the window.
        /// </summary>
        public uint uCount;

        /// <summary>
        /// The rate at which the Window is to be flashed, in milliseconds. If Zero, the function uses the default cursor blink rate.
        /// </summary>
        public uint dwTimeout;
    }

    /// <summary>
    /// 动画类型定义类
    /// </summary>
    internal class WindowAnimateType
    {
        /// <summary>
        /// 从左到右打开窗口
        /// </summary>
        public const int AW_HOR_POSITIVE = 0x00000001;

        /// <summary>
        /// 从右到左打开窗口
        /// </summary>
        public const int AW_HOR_NEGATIVE = 0x00000002;

        /// <summary>
        /// 从上到下打开窗口
        /// </summary>
        public const int AW_VER_POSITIVE = 0x00000004;

        /// <summary>
        /// 从下到上打开窗口
        /// </summary>
        public const int AW_VER_NEGATIVE = 0x00000008;

        /// <summary>
        /// 若使用了AW_HIDE标志，则使窗口向内重叠；若未使用AW_HIDE标志，则使窗口向外扩展
        /// </summary>
        public const int AW_CENTER = 0x00000010;

        /// <summary>
        /// 隐藏窗口，缺省则显示窗口
        /// </summary>
        public const int AW_HIDE = 0x00010000;

        /// <summary>
        /// 激活窗口。在使用了AW_HIDE标志后不要使用这个标志
        /// </summary>
        public const int AW_ACTIVATE = 0x00020000;

        /// <summary>
        /// 使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略
        /// </summary>
        public const int AW_SLIDE = 0x00040000;

        /// <summary>
        /// 使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志
        /// </summary>
        public const int AW_BLEND = 0x00080000;
    }
}
